package huoye.shops.huoye.mine.widget;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.codeideology.android.log.LogUtils;
import com.codeideology.android.utils.StringUtils;
import com.codeideology.android.widget.recycler.RecyclerViewAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import huoye.shops.huoye.mine.adapter.PowerSelectAdapter;
import huoye.shops.huoye.mine.model.PowerList;
import huoye.shops.huoye.mine.model.PowerSelectData;

/**
 * Created by user on 2017/9/5.
 */

public class PowerSelectRecyclerView extends RecyclerView {

    public PowerSelectRecyclerView(Context context) {
        super(context);
        init();
    }

    public PowerSelectRecyclerView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public PowerSelectRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }
    public static final int TYPE_TITLE = 0;
    PowerSelectAdapter adapter;
    public void init() {
        GridLayoutManager manager = new GridLayoutManager(getContext(), 3);
        manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                switch (position){
                    case TYPE_TITLE:
                        return 3;//第一行占3个位置（全部位置）。
                    default:
                        return 1;
                }
            }
        });
        setLayoutManager(manager);
        adapter = new PowerSelectAdapter(getContext());
        //getPowerSelectData();
       // adapter.setData(powerParts);
        adapter.setOnItemClickListener(new RecyclerViewAdapter.OnItemClickListener<PowerList>() {

            @Override
            public void onItemClick(PowerList data, int i, View view) {
                if(adapter.getValue().containsKey(data.getPowerId())) {
                    adapter.getValue().remove(data.getPowerId());
                }else {
                    adapter.getValue().put(data.getPowerId(),data);
                }
                adapter.notifyDataSetChanged();
                showPowerSelect();
                if(onPowerSelectItemClick != null) {
                    onPowerSelectItemClick.onClick();
                }
            }
        });
        setAdapter(adapter);
    }





    String mParts = "";
    String names = "";

    public String getParts() {
        return mParts;
    }

    public String getNames() {
        return names;
    }

    public void showPowerSelect() {
        names = "";
        mParts = "";
        HashMap<String, PowerList> values = adapter.getValue();
        for (String key : values.keySet()) {
            PowerList part = values.get(key);
            names += part.getPowerName() + split;
            if(StringUtils.isNotEmpty(mParts)) {
                mParts += ",";
            }
            mParts += part.getPowerId();
        }
    }

    public void setSplit(String split) {
        this.split = split;
    }

    private String split = " ";

    public void setOnPowerSelectItemClick(OnPowerSelectItemClick onPowerSelectItemClick) {
        this.onPowerSelectItemClick = onPowerSelectItemClick;
    }

    private OnPowerSelectItemClick onPowerSelectItemClick;


    public interface OnPowerSelectItemClick {
        void onClick();
    }

    public void setValue(String partBody) {
        List<PowerList> data = adapter.getData();
        for (int i = 0 ; i < data.size(); i++){
            PowerList powerList = new PowerList();
            powerList.setPowerId(data.get(i).getPowerId());
            powerList.setPowerName(data.get(i).getPowerName());
            parts.put(powerList.getPowerId(),powerList);
        }
        Map<String,PowerList> all = parts;


        if(StringUtils.isNotEmpty(partBody)) {
            String[] values = partBody.split(",");
            if(values != null) {
                for (int i = 0; i < values.length; i++) {
                    String id = values[i];
                    if(all.containsKey(id)) {
                        adapter.getValue().put(id, all.get(id));
                      //  adapter.notifyDataSetChanged();
                    }
                }
                showPowerSelect();
            }
        }
    }


    public void setPowerValue(List<PowerList> powerList) {
        adapter.setData(powerList);
    }

    private Map<String,PowerList> parts = new HashMap<>();




}
